<?php namespace testsacvoyage\storage;

use \ArrayIterator;

require_once 'testsacvoyage/storage/AbstractTest.php';
use testsacvoyage\storage\AbstractTest;

require_once 'sacvoyage/storage/Gateway.php';
use sacvoyage\storage\Gateway;

require_once 'sacvoyage/storage/Table.php';
use sacvoyage\storage\Table;

require_once 'sacvoyage/storage/Selector.php';
use sacvoyage\storage\Selector;

require_once 'sacvoyage/Object.php';
use sacvoyage\Object;




class SelectorTest extends AbstractTest {

	public function setUp() {
		$this->resetDatabase();	
		$this->fillDatabase();
	}

	/*
	 * 
	 */
	public function testSelectAllFromAll() {
		$expected=$this->removeRevs($this->getTestDocumentSet());
		$this->_sort($expected);
		
		$documents=$this->selectDocuments();
		$this->_sort($documents);
		
		$this->assertEquals($expected, $documents); 
	}

	public function testSelectSomeFromAll() {
		$expected=$this->removeRevs(array(
			new Object(array('type'=> 'note', 'priority'=>4)),
			new Object(array('type'=> 'book')),
			new Object(array('type'=> 'idea')),
			new Object(array('type'=> 'note', 'priority'=>5)),
			new Object(array('type'=> 'note', 'priority'=>5)),
			new Object(array('type'=> 'note', 'priority'=>5)),
			new Object(array('type'=> 'note', 'priority'=>'5')),
		));
		$this->_sort($expected);
		
		$documents=$this->selectDocuments(null, new Object(array('type'=>true, 'priority'=>true)));
		$this->_sort($documents);
		
		$this->assertEquals($expected, $documents);
	}

	public function testSelectAllFromSome() {
		$expected=$this->getTestDocumentSet();
		$expected=$this->removeRevs(array(
			$expected[0], $expected[2], $expected[5]
		));
		$this->_sort($expected);
		
		$documents=$this->selectDocuments(new ArrayIterator(array(
			new Object(array('_id'=>'newnote', '_rev'=>'1')),
			new Object(array('_id'=>'newidea', '_rev'=>'1')),
			new Object(array('_id'=>'newtask', '_rev'=>'2'))
		)));
		$this->_sort($documents);
		
		$this->assertEquals($expected, $documents);
	}

	public function testSelectSomeFromSome() {
		$expected=$this->getTestDocumentSet();
		$expected=$this->removeRevs(array(
			$expected[0], $expected[2], $expected[5]
		));
		$this->_sort($expected);
		
		$documents=$this->selectDocuments(new ArrayIterator(array(
			new Object(array('_id'=>'newnote', '_rev'=>'1')),
			new Object(array('_id'=>'newidea', '_rev'=>'1')),
			new Object(array('_id'=>'newtask', '_rev'=>'2'))
		)));
		$this->_sort($documents);
		
		$this->assertEquals($expected, $documents);
	}
	
	
	private function _sort(&$array) {
		$array=array_map('serialize', $array);
		sort($array);
		$array=array_map('unserialize', $array);
	}	


	public function removeRevs($documents) {
		$result=array();
		foreach($documents as $document) {
			unset($document->_rev);
			$result[]=$document;
		}
		return $result;
	}

	public function selectDocuments($array=null, $select=null) {
		return iterator_to_array( 
			new Selector(
				new Table($this->getGateway(), 'alex_db_notes'),
				$array ? new ArrayIterator($array) : null,
				$select
			)
		);
	}

}

?>
